<template>
  <div>
    <header class="header">
      <FanPageHeader
        subTitle="博客主页"
        current="返回首页"
        :img="Karina3Img"
        title="个人博客"
      />
      <!-- Waves 放在header内部，覆盖在图片下部分 -->
      <div class="waves-wrapper">
        <Waves height="6rem" />
      </div>
    </header>
    <main class="content">
      <a-row type="flex" justify="center" align="top" style="min-height: 100vh">
    <a-col :span="14" :offset="1">
      <div class="article-list-container">
        <FanArticleListItem 
          v-for="item in currentPageItems" 
          :content="item.summary"
          :key="item.id"
          :img="item.img" 
          :title="item.title"
          :publishTime="item.pubLishTime"
          :views="item.views"
          :tags="item.tags.split(' ')"
          @click="handleClick(item.id,item.img,item.title,item.views)"
        />
      </div>
    </a-col>
    <a-col :span="5" :offset="1">
      <FanArticleAside 
        v-if="gonggao.id"
        :title="gonggao.title"
        :icon="gonggao.icon"
        :text="gonggao.text"
        :canClick="gonggao.canClick"
      />
      
      <FanArticleAside 
      title="标签"
      icon="#icon-biaoqianguanli"
      >
        <div class="icons">
          <div 
            v-for="(item, index) in webList" 
            :key="item.id"
            class="icons-item"
            @click="handleTagClick(item)"
          >
            <span class="icon-text">{{ item.name }}</span>
            <svg class="icon" aria-hidden="true">
              <use :xlink:href="item.icon"></use>
            </svg>
          </div>
        </div>
      </FanArticleAside>
      <!-- 热门文章 -->
      <FanArticleAside 
        class="hot-article"
        v-if="hotArticle.id"
        :views="hotArticle.views"
        :key="hotArticle.id"
        :title="hotArticle.title"
        :icon="hotArticle.icon"
        :text="hotArticle.text"
        :canClick="hotArticle.canClick"
        :route="hotArticle.route"
        :img="hotArticle.img"
      />
    </a-col>
  </a-row>
  
  <!-- 分页组件 -->
  <div class="pagination-container">
    <a-pagination 
      v-model:current="currentPage" 
      :total="articleList.length" 
      :pageSize="pageSize"
      @change="handlePageChange"
    />
  </div>
  </main>
  </div>
</template>

<script setup>
import Karina3Img from '../../assets/wrapper/karina1.png';
import {ref, onMounted,computed } from 'vue';
import { useRouter } from 'vue-router';
const router = useRouter();
import { useArticleStore } from '../../stores/article';
const articleStore = useArticleStore();
import FanPageHeader from '../../components/common/FanPageHeader.vue';
import Waves from "@/components/common/waves.vue";
import FanArticleListItem from '../../components/FanArticleListItem.vue';
import FanArticleAside from '../../components/common/FanArticleAside.vue';
import { FanNotification } from '../../libs/util.toast';
const webList=ref([
  {
    id: 1,
    name: 'vue',
    icon: '#icon-Vue',
    web:'https://cn.vuejs.org/'
  },
  {
    id: 2,
    name: 'nodejs',
    icon: '#icon-Nodejs',
    web: 'https://node.org.cn/en' 
  },
  {
    id: 3,
    name: 'Typescript',
    icon: '#icon-ts',
    web: 'https://www.typescriptlang.org/' 
  },
  {
    id: 4,
    name: '生活',
    icon: '#icon-shenghuo',
    web: 'https://www.instagram.com/'
  },
  {
    id: 5,
    name: 'axios',
    icon: '#icon-axios',
    web: 'https://www.axios-http.cn/'
  },
  {
    id: 6,
    name: '篮球',
    icon: '#icon-lanqiu',
    web: 'https://china.nba.cn/'
  },
  {
    id: 7,
    name: 'github',
    icon: '#icon-github',
    web: 'https://github.com/'
  },
  {
    id: 8,
    name: 'uview-pro',
    icon: '#icon-view',
    web: 'https://uviewpro.cn/'
  },
  {
    id: 9,
    name: 'ant-design-vue',
    icon: '#icon-antd',
    web: 'https://3x.antdv.com/components/overview-cn'
  },
  {
    id: 10,
    name: 'vant',
    icon: '#icon-vant',
    web: 'https://vant4.ylhtest.com/#/zh-CN'
  },
  {
    id: 11,
    name: 'bugAPI',
    icon: '#icon-liebiaotubiao_bug',
    web: 'https://api.bugpk.com/'
  }, {
    id: 12,
    name: 'cloudflare',
    icon: '#icon-cloudflare',
    web: 'https://www.cloudflare.com/'
  }, {
    id: 12,
    name: 'bootstrap',
    icon: '#icon-bootstrap',
    web: 'https://getbootstrap.com/'
  },
  {
    id: 13,
    name: 'electron',
    icon: '#icon-electron',
    web: 'https://www.electronjs.org/'
  },
  {
    id: 14,
    name: 'element-plus',
    icon: '#icon-element-plus',
    web: 'https://element-plus.org/zh-CN/#/zh-CN'
  },
  {
    id: 15,
    name: 'echarts',
    icon: '#icon-ECharts',
    web: 'https://demo.runoops.com/echarts-website/zh/index.html'
  },
  {
    id: 16,
    name: '火山云',
    icon: '#icon-huoshanyun',
    web: 'https://www.volcengine.com/'
  },
  {
    id: 17,
    name: 'gitee',
    icon: '#icon-gitee',
    web: 'https://gitee.com/'
  },
  {
    id: 18,
    name: 'uni-app',
    icon: '#icon-uniapp',
    web: 'https://uniapp.dcloud.net.cn/'
  },
  {
    id:19,
    name: '即时设计',
    icon: '#icon-a-yemian11',
    web: 'https://js.design/'
  },
  {
    id: 20,
    name: 'Nuxt',
    icon: '#icon-Nuxt',
    web: 'https://nuxtjs.org.cn/'
  },
  {
    id: 21,
    name: 'gemini',
    icon: '#icon-gemini',
    web: 'https://gemini.google.com/'
  }
])

// 处理标签点击事件
const handleTagClick = (item) => {
  if (item.web && item.web.trim() !== '') {
    // 在新标签页打开链接
    window.open(item.web, '_blank')
  } else {
    // 如果没有web链接，可以显示提示或者执行其他逻辑
    console.log(`点击了标签: ${item.name}`)
    // 这里可以添加其他处理逻辑，比如筛选文章等
  }
}
const handleClick = (id, img,title,views) => {
    articleStore.img = img;
    articleStore.title = title;
    articleStore.subTitle = views+'次阅读';
  router.push({ name: 'blogDetail', params: { id } });
};
// 模拟侧边栏数据
const gonggao = ref({
  id: 1,
  title: '公告',
  icon: '#icon-gonggao',
  text: '后台系统和评论系统加急开发中！',
  canClick: false,
});
//热门文章的数据  
const hotArticle = ref({
  id:0,
  text:'',
  img:'',
  views:0,
})

const articleList = ref([]);
// 分页状态
const currentPage = ref(1);
const pageSize = 3; // 每页显示3篇文章

const getArticleList = async () => { 
  const res=await articleStore.getArticleList();
  if (res.success) {
    articleList.value = res.data.list;
    console.log(articleList.value);
    FanNotification.success(`获取文章列表成功，共${res.data.list.length}篇文章`);
  }
  else {
    FanNotification.error('获取文章列表失败');
    articleList.value = []; // 失败时返回空数组
  }
}
// 计算当前页显示的文章
const currentPageItems = computed(() => {
  const start = (currentPage.value - 1) * pageSize;
  const end = start + pageSize;
  return articleList.value.slice(start, end);
});

// 总页数
const totalPages = computed(() => Math.ceil(articleList.value.length / pageSize));

// 分页变化处理
const handlePageChange = (page) => {
  currentPage.value = page;
};
const getHotArticle = async () => {
  const res = await articleStore.getHotArticle();
  if (res.success) {
    hotArticle.value = res.data;
    hotArticle.value.icon = '#icon-remen';
    hotArticle.value.title = '热门文章';
    hotArticle.value.canClick = true;
    hotArticle.value.route = { name: 'blogDetail', params: { id: hotArticle.value.id } }
  }
  else {
    hotArticle.value = {}; // 失败时返回空对象
  }
}
onMounted(() => {
  getArticleList();
  getHotArticle();
});
</script>

<style scoped lang="scss">
/* 页面整体布局 */
.page {
  min-height: 100vh;

}
.header {
  position: relative;
  width: 100%;
  flex-shrink: 0;
}
/* Waves 包装器 - 确保波浪只覆盖图片下部分 */
.waves-wrapper {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 10rem;
  pointer-events: none; /* 确保不拦截鼠标事件 */
}

/* 内容区域样式 */
.content {
  flex: 1;
  padding: 40px 0;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  .hot-article {
cursor: pointer;
  }
  .icon {
    width: 2.6rem;
    height: 2.6rem;
    vertical-align: -0.15em;
    fill: currentColor;
    overflow: hidden;
  }
}

/* 响应式布局 */
@media (max-width: 1200px) {
  .content {
    padding: 20px 10px;
  }
}

@media (max-width: 768px) {
  .content {
    padding: 10px 5px;
  }
}

/* 标签组合样式 */
.icons {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  padding: 10px 0;
}

.icons-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 20px;
  color: white;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
  position: relative;
  overflow: hidden;
}

.icons-item::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.5s;
}

.icons-item:hover::before {
  left: 100%;
}

.icons-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(102, 126, 234, 0.4);
}

.icons-item:active {
  transform: translateY(0);
}

.icons-item .icon-text {
  font-weight: 600;
  letter-spacing: 0.5px;
}

.icons-item .icon {
  width: 18px;
  height: 18px;
  fill: white;
  transition: transform 0.3s ease;
}

.icons-item:hover .icon {
  transform: scale(1.1);
}

/* 标签动画效果 */
.icons-item {
  animation: fadeInUp 0.6s ease-out;
  animation-fill-mode: both;
}

.icons-item:nth-child(1) { animation-delay: 0.7s; }
.icons-item:nth-child(2) { animation-delay: 1.2s; }
.icons-item:nth-child(3) { animation-delay: 1.6s; }
.icons-item:nth-child(4) { animation-delay: 1.9s; }
.icons-item:nth-child(5) { animation-delay: 2.3s; }
.icons-item:nth-child(6) { animation-delay: 2.7s; }

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 不同标签的不同渐变色 */
.icons-item:nth-child(1) {
  background: linear-gradient(135deg, #1c749c 0%, #0c835b 100%);
  box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
}

.icons-item:nth-child(2) {
  background: linear-gradient(135deg, #129764 0%, #d7e7d4 100%);
  box-shadow: 0 2px 8px rgba(240, 147, 251, 0.3);
}

.icons-item:nth-child(3) {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
  box-shadow: 0 2px 8px rgba(79, 172, 254, 0.3);
}

.icons-item:nth-child(4) {
  background: linear-gradient(135deg, #ddafdf 0%, #751651 100%);
  box-shadow: 0 2px 8px rgba(67, 233, 123, 0.3);
}

.icons-item:nth-child(5) {
  background: linear-gradient(135deg, #fa709a 0%, #fee140 100%);
  box-shadow: 0 2px 8px rgba(250, 112, 154, 0.3);
}

.icons-item:nth-child(6) {
  background: linear-gradient(135deg, #a8edea 0%, #fed6e3 100%);
  box-shadow: 0 2px 8px rgba(168, 237, 234, 0.3);
  color: #333;
}

.icons-item:nth-child(6) .icon {
  fill: #333;
}

/* 响应式标签样式 */
@media (max-width: 768px) {
  .icons {
    gap: 8px;
  }
  
  .icons-item {
    padding: 6px 12px;
    font-size: 12px;
    gap: 4px;
  }
  
  .icons-item .icon {
    width: 16px;
    height: 16px;
  }
}

/* 分页容器样式 */
.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 1rem;
  padding: 1.5rem 0;
}

/* 文章列表容器 */
.article-list-container {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

/* 移动端优化 */
@media (max-width: 768px) {
  .article-list-container {
    gap: 1rem; /* 减少移动端卡片间距 */
  }
}/* 减少卡片间距 */

</style>